<template>
  <div class="box">
    <el-form size="medium" ref="itemForm" :model="obj" :rules="itemRules" label-width="100px">
      <el-form-item label="用户昵称">{{ nickName }}</el-form-item>

      <el-form-item label="会员等级">
        <el-select v-model="obj.level"><el-option v-for="(item, i) in list" :key="item.level" :label="item.name" :value="item.level"></el-option></el-select>
      </el-form-item>
      <el-form-item label="永久有效">
        <el-radio-group v-model="alltime">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="有效期" v-if="!alltime">
        <el-date-picker v-model="obj.level_time" value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择有效期"></el-date-picker>
      </el-form-item>
      <el-form-item label="管理员密码" prop="password"><el-input placeholder="请输入操作员密码" show-password v-model="obj.password"></el-input></el-form-item>

      <el-form-item><el-button type="primary" @click="saveData">保存数据</el-button></el-form-item>
    </el-form>
  </div>
</template>
<script>
import {getuserdetail, editUserLevel} from '@/api/mall/user';

export default {
  name: 'editMoney',
  data() {
    return {
      isLoading: false,
      nickName: '',
      list: [{name: '普通用户', level: 0}],
      obj: {
        id: '',
        level: 0,
        password: '',
        level_time: ''
      },
      alltime: 1,
      itemRules: {
        remark: [
          {
            required: true,
            message: '请输入备注'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入管理员密码'
          }
        ],
        amount: [
          {
            required: true,
            message: '请输入变更金额'
          }
        ]
      }
    };
  },
  created() {},
  methods: {
    //初始化
    init(id, list) {
      this.list = this.list.concat(list);
      this.isEdit = true;
      this.isLoading = true;
      getuserdetail({id})
        .then(res => {
          this.isLoading = false;
          let o = res.msg;
          //赋值数据
          this.nickName = o.nickname;

          this.obj.id = o.id;
          this.obj.level = o.level;
          this.obj.level_time = o.level_time;
          this.alltime =!!!o.level_time;
          console.log(this.alltime)
        })
        .catch(e => {
          this.isLoading = false;
        });
    },
    //保存数据(修改发货地址)
    saveData() {
      this.$refs.itemForm.validate(valid => {
        if (valid) {
          let o = {...this.obj};
          if(this.alltime){
            o.level_time=''
          }
          this.isLoading = true;
          editUserLevel(o)
            .then(res => {
              this.isLoading = false;
              this.$emit('success');
            })
            .catch(e => {
              this.isLoading = false;
            });
        } else {
          //console.log('error')
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 15px;
}
</style>
